package com.ftqh.tutorial.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ftqh.tutorial.Entity.User;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/4/27.
 */
@RestController
public class ProviderController {
    private final Logger logger = Logger.getLogger(getClass());
    @Autowired
    private ServerProperties serverProperties;
    @Autowired
    private DiscoveryClient client;
    @Autowired
    private Registration registration;

    @GetMapping("/add")
    @ApiOperation(value = "算数计算", notes = "加法运算", code = 200, produces = "application/text")
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        //ServiceInstance instance = client.getLocalServiceInstance();
        List<ServiceInstance> serviceInstances = client.getInstances(registration.getServiceId());
        Integer r = a + b;
        serviceInstances.stream()
                .filter(instance -> serverProperties.getPort().equals(instance.getPort()))
                .forEach(instance -> logger.info("/add, host:" + instance.getHost() + ":"+instance.getPort()+", service_id:" + instance.getServiceId() + ", result:" + r));
        return r;
    }
    @GetMapping("/users/{id}")
    @ApiOperation(value = "获取用户", notes = "根据ID获取指定用户", code = 200, produces = "application/json")
    public User getUser(@PathVariable("id") String id) {
        List<ServiceInstance> serviceInstances = client.getInstances(registration.getServiceId());
        serviceInstances.stream()
                .filter(instance -> serverProperties.getPort().equals(instance.getPort()))
                .forEach(instance -> logger.info("/add, host:" + instance.getHost() + ":"+instance.getPort()+", service_id:" + instance.getServiceId()));
        User user = new User();
        user.setId(Integer.valueOf(id));
        user.setName("zhangsam");
        user.setGender(1);
        return user;
    }
    @PostMapping("/users")
    public JSONArray getUsers(String[] ids) {
        List<ServiceInstance> serviceInstances = client.getInstances(registration.getServiceId());
        serviceInstances.stream()
                .filter(instance -> serverProperties.getPort().equals(instance.getPort()))
                .forEach(instance -> logger.info("/users, host:" + instance.getHost() + ":"+instance.getPort()+", service_id:" + instance.getServiceId()));
        JSONArray users = new JSONArray();
        for (String id:ids) {
            JSONObject user = new JSONObject();
            user.put("id", id);
            user.put("name", "zhangsan" + id);
            users.add(user);
        }
        return users;
    }

    @PostMapping("/users1")
    public JSONArray getUsers1(@RequestBody JSONObject idsJson) {
        List<ServiceInstance> serviceInstances = client.getInstances(registration.getServiceId());
        serviceInstances.stream()
                .filter(instance -> serverProperties.getPort().equals(instance.getPort()))
                .forEach(instance -> logger.info("/users1, host:" + instance.getHost() + ":"+instance.getPort()+", service_id:" + instance.getServiceId()));
        JSONArray users = new JSONArray();
        for (Integer id:(List<Integer>)idsJson.get("ids")) {
            JSONObject user = new JSONObject();
            user.put("id", id);
            user.put("name", "zhangsan" + id);
            users.add(user);
        }
        return users;
    }

    @PostMapping("/users2")
    public JSONArray getUsers2(@RequestBody Integer[] ids) {
        List<ServiceInstance> serviceInstances = client.getInstances(registration.getServiceId());
        serviceInstances.stream()
                .filter(instance -> serverProperties.getPort().equals(instance.getPort()))
                .forEach(instance -> logger.info("/users2, host:" + instance.getHost() + ":"+instance.getPort()+", service_id:" + instance.getServiceId()));
        JSONArray users = new JSONArray();
        for (Integer id:ids) {
            JSONObject user = new JSONObject();
            user.put("id", id);
            user.put("name", "zhangsan" + id);
            users.add(user);
        }
        return users;
    }
    @PostMapping("/users/bytes")
    public String getUsers2(@RequestBody Byte[] file) {
        System.out.println(file);
        return "success";
    }
}
